package _4_multithread._1_basic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author bpzj 2019/11/22 13:54
 */
public class ThreadPoolStudy {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(16);
        // 创建多个有返回值的任务
        List<Future> list = new ArrayList<Future>();
        for (int i = 0; i < 16; i++) {
            Callable c = new MyCallable(i + " ");
            // 执行任务并获取 Future 对象
            Future f = pool.submit(c);
            list.add(f);
        }
        // 关闭线程池
        pool.shutdown();
        // 获取所有并发任务的运行结果
        for (Future f : list) {
            // 从 Future 对象上获取任务的返回值，并输出到控制台
            System.out.println("res：" + f.get().toString());
        }
    }

    private static class MyCallable implements Callable {
        public MyCallable(String s) {
        }

        @Override
        public Object call() throws Exception {
            return "null";
        }
    }
}
